package com.lifang.imsoa.im.apidemo;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.lifang.imsoa.im.comm.HTTPMethod;
import com.lifang.imsoa.im.utils.JerseyUtils;
import com.lifang.imsoa.im.vo.Credential;
import com.lifang.imsoa.im.vo.EndPoints;
import com.lifang.imsoa.util.Global;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.glassfish.jersey.client.JerseyWebTarget;

import java.util.ArrayList;
import java.util.List;

@Slf4j
public class EasemobIMUsers {

    private static JsonNodeFactory factory = new JsonNodeFactory(false);


    /**
     * 检查AppKey格式是否正确
     */
    private static boolean checkAppKeyFormat(String APPKEY, ObjectNode objectNode) {
        if (!JerseyUtils.match("^(?!-)[0-9a-zA-Z\\-]+#[0-9a-zA-Z]+", APPKEY)) {
            log.error("Bad format of Appkey: " + APPKEY);
            objectNode.put("message", "Bad format of Appkey");
            return true;
        }
        return false;
    }

    /**
     * 注册IM用户[批量]
     * <p/>
     * 给指定AppKey创建一批用户
     *
     * @param dataArrayNode
     * @return
     */
    public static ObjectNode createNewIMUserBatch(ArrayNode dataArrayNode, String APPKEY, Credential credential) {
        ObjectNode objectNode = factory.objectNode();

        // check appKey format
        if (checkAppKeyFormat(APPKEY, objectNode)) return objectNode;

        // check properties that must be provided
        if (dataArrayNode.isArray()) {
            for (JsonNode jsonNode : dataArrayNode) {
                if (null != jsonNode && !jsonNode.has("username")) {
                    log.error("Property that named username must be provided .");
                    objectNode.put("message", "Property that named username must be provided .");
                    return objectNode;
                }
                if (null != jsonNode && !jsonNode.has("password")) {
                    log.error("Property that named password must be provided .");
                    objectNode.put("message", "Property that named password must be provided .");
                    return objectNode;
                }
            }
        }
        JerseyWebTarget webTarget = EndPoints.USERS_TARGET
                .resolveTemplate("org_name", APPKEY.split("#")[0])
                .resolveTemplate("app_name", APPKEY.split("#")[1]);
        objectNode = JerseyUtils.sendRequest(webTarget, dataArrayNode, credential, HTTPMethod.METHOD_POST, null);
        return objectNode;
    }

    /**
     * 获取IM用户
     *
     * @param hxId 用户主键：hxId
     * @return
     */
    public static ObjectNode getIMUsersByPrimaryKey(String hxId, String APPKEY, Credential credential) {
        ObjectNode objectNode = factory.objectNode();
        // check appKey format
        if (checkAppKeyFormat(APPKEY, objectNode)) return objectNode;
        // check properties that must be provided
        if (StringUtils.isEmpty(hxId)) {
            log.error("环信ID必须提供");
            objectNode.put("message", "环信ID必须提供");
            return objectNode;
        }
        JerseyWebTarget webTarget = EndPoints.USERS_TARGET
                .resolveTemplate("org_name", APPKEY.split("#")[0])
                .resolveTemplate("app_name", APPKEY.split("#")[1])
                .path(hxId);

        objectNode = JerseyUtils.sendRequest(webTarget, null, credential, HTTPMethod.METHOD_GET, null);
        return objectNode;
    }

    public static ObjectNode selectHxUserOnlineStatus(String hxId,String APPKEY, Credential credential) {
        ObjectNode objectNode = factory.objectNode();
        // check appKey format
        if (checkAppKeyFormat(APPKEY, objectNode)) return objectNode;
        // check properties that must be provided
        if (StringUtils.isEmpty(hxId)) {
            log.error("环信ID必须提供");
            objectNode.put("message", "环信ID必须提供");
            return objectNode;
        }
        JerseyWebTarget webTarget = EndPoints.HXUSER_ONLINE
                .resolveTemplate("org_name", APPKEY.split("#")[0])
                .resolveTemplate("app_name", APPKEY.split("#")[1])
                .resolveTemplate("username", hxId);

        objectNode = JerseyUtils.sendRequest(webTarget, null, credential, HTTPMethod.METHOD_GET, null);
        return objectNode;
    }

}
